package com.ph.monitorPlatform.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ph.monitorPlatform.entity.User;
import com.ph.monitorPlatform.entity.View;
import com.ph.monitorPlatform.mapper.ViewMapper;
import com.ph.monitorPlatform.service.IViewService;
import com.ph.monitorPlatform.utils.JsonResult;
import com.ph.monitorPlatform.utils.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zuohongyu
 * @since 2020-04-09
 */
@Service
public class ViewServiceImpl extends ServiceImpl<ViewMapper, View> implements IViewService {

    @Autowired
    private ViewMapper viewMapper;
    @Autowired
    private UserUtil userUtil;

    @Override
    public JsonResult copyView(int viewId) {
        View view = viewMapper.selectByViewId(viewId);
        if (view == null) {
            return JsonResult.failMessage("视图不存在，请重新选择");
        }
        User currentUser = userUtil.getCurrentUser();
        View copyView = new View(view.getViewName() + "-复制",view.getViewTitle()+ "-复制",view.getViewType(),
                view.getProtectLevel(),view.getContent(),currentUser.getUserId(),LocalDateTime.now(),view.getViewImg());
        int result = viewMapper.insert(copyView);
        if (result != 1) {
            return JsonResult.failMessage("复制失败");
        } else {
            return JsonResult.successMessage("复制成功");
        }
    }
}
